//
//  OrderSuccessViewController.m
//  Haliaeetus
//
//  Created by junli hou on 2018/4/30.
//  Copyright © 2018年 BMY. All rights reserved.
//

#import "OrderSuccessViewController.h"
#import "TravelDetialViewController.h"
@interface OrderSuccessViewController ()
@property (nonatomic, strong) UIButton *goHomeBtn;
@property (nonatomic, strong) UIButton *scheduleBtn;
@property (nonatomic, strong) UIButton *wechatShareBtn;
@property(nonatomic,strong) UILabel *notice;
@end

@implementation OrderSuccessViewController
- (instancetype)initWithReloadHomeUrlBlock:(ReloadHomeUrlBlock)reloadHomeUrlBlock
{
    self = [super init];
    if (self) {
        _reloadHomeUrlBlock = reloadHomeUrlBlock;
        
    }
    return self;
}
- (void)loadView {
    [super loadView];
    self.title =@"房间预定";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.navigationController setLeftBtnWithTarget:self action:@selector(goBack)];
    [self initView];
    
}
- (void)goBack {
     self.reloadHomeUrlBlock();
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
       [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateWXShare:) name:@"UpdateWXShare"object:Nil];
}
- (void)initView{
    
    
    
    UIImageView *successImageView = [[UIImageView alloc]init];
    successImageView.frame= CGRectMake((WS_SCREEN_FRAME_WIDTH-50)/2,76,50,50);
    successImageView.image = [UIImage imageNamed:@"pay_success"];
    successImageView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:successImageView];
    
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(successImageView.frame)+10,WS_SCREEN_FRAME_WIDTH,26)];
    titleLabel.text = @"预约成功";
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor = WS_RGBA(51,51,51,1);
    titleLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:titleLabel];
    
    UILabel *noticeLabel = [[UILabel alloc]initWithFrame:CGRectMake((WS_SCREEN_FRAME_WIDTH-198)/2,CGRectGetMaxY(titleLabel.frame)+7,198,34)];
    noticeLabel.text = @"按照您的要求已成功预约所需酒店，确认预约成功方式有2种:";
    noticeLabel.textAlignment=NSTextAlignmentCenter;
    noticeLabel.textColor = WS_RGBA(51,51,51,1);
    noticeLabel.numberOfLines=2;
    noticeLabel.font = [UIFont systemFontOfSize:10];
    [self.view addSubview:noticeLabel];
    
    
    UIView *pointView1 = [[UIView alloc]initWithFrame:CGRectMake(27,CGRectGetMaxY(noticeLabel.frame)+58,5,5)];
    pointView1.layer.cornerRadius=2.5;
    pointView1.layer.masksToBounds = YES;
    pointView1.backgroundColor = WS_RGB(93, 93, 93);
    [self.view addSubview:pointView1];
    
    UITextView *noticeLabel1 = [[UITextView alloc]initWithFrame:CGRectMake(42,CGRectGetMaxY(noticeLabel.frame)+44,WS_SCREEN_FRAME_WIDTH-42-26,48)];
    noticeLabel1.text = @"您的邮箱将收到酒店预约确认信息。";
    noticeLabel1.textAlignment=NSTextAlignmentLeft;
    noticeLabel1.textColor = WS_RGBA(102,102,102,1);
    noticeLabel1.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:noticeLabel1];
    
    UIView *pointView2 = [[UIView alloc]initWithFrame:CGRectMake(27,CGRectGetMaxY(pointView1.frame)+31,5,5)];
    pointView2.layer.cornerRadius=2.5;
    pointView2.layer.masksToBounds = YES;
    pointView2.backgroundColor = WS_RGB(93, 93, 93);
    [self.view addSubview:pointView2];
    
    UITextView *noticeLabel2 = [[UITextView alloc]initWithFrame:CGRectMake(42,CGRectGetMaxY(noticeLabel.frame)+82,WS_SCREEN_FRAME_WIDTH-42-26,48)];
    noticeLabel2.text = @"如过没有收到邮件，可查看行程详情拨打酒店电话到酒店前台查询。";
    noticeLabel2.textAlignment=NSTextAlignmentLeft;
    noticeLabel2.textColor = WS_RGBA(102,102,102,1);
    noticeLabel2.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:noticeLabel2];
    
    
    
    self.scheduleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.scheduleBtn.frame = CGRectMake(15,CGRectGetMaxY(noticeLabel2.frame) + 40,self.view.frame.size.width-40,40);
    //    _LoginBtn.frame = CGRectMake(20,self.view.frame.size.height - 90 - 40,self.view.frame.size.width-40,40);
    self.scheduleBtn.userInteractionEnabled = YES;
    [self.scheduleBtn setTitle:@"行程详情" forState:UIControlStateNormal];
    self.scheduleBtn.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    [self.scheduleBtn setTitleColor:WS_RGB(255, 255, 255) forState:UIControlStateNormal];
    self.scheduleBtn.layer.masksToBounds = YES;
    self.scheduleBtn.layer.cornerRadius = 3;
    self.scheduleBtn.layer.borderWidth = 1;
    self.scheduleBtn.layer.borderColor = [WS_RGBA(155, 155, 155, 1)  CGColor];
    self.scheduleBtn.backgroundColor = WS_RGB(255, 134, 35);
    [self.scheduleBtn addTarget:self action:@selector(goScheduleDetail) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.scheduleBtn];

    
    

    

    self.goHomeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.goHomeBtn.frame = CGRectMake(15,CGRectGetMaxY(self.scheduleBtn.frame) + 20,self.view.frame.size.width-40,40);
    //    _LoginBtn.frame = CGRectMake(20,self.view.frame.size.height - 90 - 40,self.view.frame.size.width-40,40);
    self.goHomeBtn.userInteractionEnabled = YES;
    [self.goHomeBtn setTitle:@"返回首页" forState:UIControlStateNormal];
    self.goHomeBtn.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    [self.goHomeBtn setTitleColor:WS_RGBA(68, 69, 60, 1) forState:UIControlStateNormal];
    self.goHomeBtn.layer.masksToBounds = YES;
    self.goHomeBtn.layer.cornerRadius = 3;
    self.goHomeBtn.layer.borderWidth = 1;
    self.goHomeBtn.layer.borderColor = [WS_RGBA(155, 155, 155, 1)  CGColor];
    [self.goHomeBtn addTarget:self action:@selector(goHome) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.goHomeBtn];
    
    
    if (self.isFirst) {
        self.wechatShareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.wechatShareBtn.frame = CGRectMake(15,CGRectGetMaxY(self.goHomeBtn.frame) + 20,self.view.frame.size.width-40,40);
        //    _LoginBtn.frame = CGRectMake(20,self.view.frame.size.height - 90 - 40,self.view.frame.size.width-40,40);
        self.wechatShareBtn.userInteractionEnabled = YES;
        [self.wechatShareBtn setTitle:@"分享至朋友圈" forState:UIControlStateNormal];
        [self.wechatShareBtn setImage:[UIImage imageNamed:@"w_friend"] forState:UIControlStateNormal];
        self.wechatShareBtn.titleLabel.font = [UIFont systemFontOfSize: 16.0];
        [self.wechatShareBtn setTitleColor:WS_RGBA(68, 69, 60, 1) forState:UIControlStateNormal];
        self.wechatShareBtn.layer.masksToBounds = YES;
        self.wechatShareBtn.layer.cornerRadius = 3;
        self.wechatShareBtn.layer.borderWidth = 1;
        self.wechatShareBtn.layer.borderColor = [WS_RGBA(155, 155, 155, 1)  CGColor];
        [self.wechatShareBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
//        [self.view addSubview:self.wechatShareBtn];
        
        self.notice = [[UILabel alloc]init];
        self.notice.frame = CGRectMake(42,CGRectGetMaxY(self.wechatShareBtn.frame)+20,WS_SCREEN_FRAME_WIDTH-42-26,40);
        self.notice.font = [UIFont systemFontOfSize:14];
        self.notice.numberOfLines=1;
        self.notice.textAlignment=NSTextAlignmentCenter;
        self.notice.text=[NSString stringWithFormat:@"首笔订单成功分享至朋友圈，返还%@元手续费",[NSString stringWithFormat:@"%.2f",[[self.dataDic objectForKey:@"commission"]floatValue]]];
        self.notice.textColor = WS_RGB(68, 69, 60);
//        [self.view addSubview:self.notice];
    }
   
    
    
    
}

-(void)shareAction{

    if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
        
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        WXMediaMessage *message = [WXMediaMessage message];
        message.title = @"选酒店就来北美游！";
        message.description = @"优惠多多多多多多!";
        [message setThumbImage:[UIImage imageNamed:@"AppIcon"]];
        req.message = message;
        WXAppExtendObject *ext = [WXAppExtendObject object];
        ext.url = @"https://www.baidu.com";
        ext.extInfo = @"Hi北美游";
        message.mediaObject = ext;
        //默认是Session分享给朋友,Timeline是朋友圈,Favorite是收藏
        req.scene = WXSceneTimeline;
        
        [WXApi sendReq:req];
    } else {
        WSToast(@"分享失败!");
    }
    
}

-(void)goHome
{
    self.reloadHomeUrlBlock();
    [self goBack];
}
- (void)updateWXShare:(NSNotification*) notification
{
    
    BaseResp* resp=notification.object;
    
    switch (resp.errCode) {
        case WXSuccess:{
            [self getShareCallback];

        }
            
            break;
        case WXErrCodeCommon:
            
            
            
            break;
        case WXErrCodeUserCancel:
            
            
            
            break;
        case WXErrCodeSentFail:
            
            
            
            break;
        default:
            
            
            break;
    }
}

#pragma mark - 分享成功回调
- (void)getShareCallback
{
    
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setValue:@"success" forKey:@"share_flag"];
    [WSNetwork WSHttpsByType:@"POST" Url:[NSString stringWithFormat:@"%@/schedules/%@/share_callback",BMY_URL,[self.dataDic objectForKey:@"schedule_id"]] params:params onCompletion:^(NSMutableDictionary *dic){
        [SVProgressHUD dismiss];
        
        
        if ([[dic objectForKey:@"biz_action"] intValue] == 0) {
            
            WSToast([dic objectForKey:@"biz_msg"]);
            [self performSelector:@selector(goBack) withObject:nil afterDelay:1.0];
        }else{
            
            //请求出错
            WSToast([dic objectForKey:@"biz_msg"]);
        }
    } onError:^(NSError *error) {
        
        if (error.code==99999) {
            WSToast(@"网络异常，请检查网络设置!");
        }
        
        
    }];
    
}
- (void)goScheduleDetail
{

    
    [SVProgressHUD showWithStatus:@"loading"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    [WSNetwork WSHttpsByType:@"GET" Url:[NSString stringWithFormat:@"%@/schedules/%@",BMY_URL,[self.dataDic objectForKey:@"schedule_id"]] params:NULL onCompletion:^(NSMutableDictionary *dic){
        [SVProgressHUD dismiss];
        
        if ([[dic objectForKey:@"biz_action"] intValue] == 0) {
         NSDictionary *DataDic=(NSDictionary*) [dic objectForKey:@"data"];
            TravelDetialViewController *travelDetialViewController=[[TravelDetialViewController alloc]init];
            travelDetialViewController.hidesBottomBarWhenPushed=YES;
            travelDetialViewController.dic=DataDic;
            [self.navigationController pushViewController:travelDetialViewController animated:YES];
            self.reloadHomeUrlBlock();
            [self removeFromParentViewController];
        }else{
            
            //请求出错
            WSToast([dic objectForKey:@"biz_msg"]);
        }
    } onError:^(NSError *error) {
        
        if (error.code==99999) {
            WSToast(@"网络异常，请检查网络设置!");
        }
        [SVProgressHUD dismiss];
        
    }];
    
}
-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
